package net.hn.hnms.biz.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.material.domain.StockOut;
import net.hn.hnms.biz.material.mapper.StockOutMapper;
import net.hn.hnms.biz.material.request.StockOutReq;
import net.hn.hnms.biz.material.service.IStockOutService;
import net.hn.hnms.biz.material.vo.StockOutVO;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ly
 * 出库服务层实现
 * @date 2022-08-26
 */

@Service
public class StockOutServiceImpl implements IStockOutService {

    private StockOutMapper stockOutMapper;

    public StockOutServiceImpl(StockOutMapper stockOutMapper) {
        this.stockOutMapper = stockOutMapper;
    }

    @Override
    public ResponsePage<StockOutVO> page(StockOutReq req, PageQuery pageQuery) {
        Page<StockOut> page = stockOutMapper.selectPage(pageQuery.build(), buildQuery(req));

        List<StockOutVO> list =page.getRecords().stream().map(o ->{
            StockOutVO vo = new StockOutVO();
            BeanUtil.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());
        return new ResponsePage<>(list, (int) page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<StockOut> 查询条件
     */
    private LambdaQueryWrapper<StockOut> buildQuery(StockOutReq req){
        return new LambdaQueryWrapper<StockOut>()
                .like(StrUtil.isNotBlank(req.getSpecificationModel()), StockOut::getSpecificationModel, req.getSpecificationModel())
                .like(StrUtil.isNotBlank(req.getStockName()), StockOut::getStockName, req.getStockName())
                .like(StrUtil.isNotBlank(req.getMaterialName()), StockOut::getMaterialName, req.getMaterialName())
                .like(StrUtil.isNotBlank(req.getMaterialCode()), StockOut::getMaterialCode, req.getMaterialCode())
                .like(StrUtil.isNotBlank(req.getMaterialCode()), StockOut::getMaterialCode, req.getMaterialCode())
                .eq(StrUtil.isNotBlank(req.getMineCode()), StockOut::getMineCode, req.getMineCode())
                ;
    }

}
